package com.hsjk.szwj.module.chuangkit.controller.admin;

import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.CopyDesignReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.CopyDesignRespVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.DeleteDesignReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.GetUserDesignsReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.GetUserDesignsRespVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TemplateCancelCollectionReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TemplateCollectionListReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TemplateCollectionListRespVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TemplateCollectionReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TemplateForApiUserReqVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TemplateForApiUserRespVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.TokenRespVO;
import com.hsjk.szwj.module.chuangkit.controller.admin.vo.UpdateTitleReqVO;
import com.hsjk.szwj.module.chuangkit.service.IChuangKitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.hsjk.szwj.framework.common.pojo.CommonResult.success;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/09/26 22:09
 **/
@Tag(name = "管理后台 - 创客贴接口")
@RestController
@RequestMapping("/system/chuang-kit")
@Validated
public class AdminChuangKitController {
    @Resource
    private IChuangKitService chuangKitService;

    @GetMapping("/get-token")
    @Operation(summary = "获取token")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    public CommonResult<TokenRespVO> getApiToken() {
        return success(chuangKitService.getApiToken());
    }

    @GetMapping("/refresh-token")
    @Operation(summary = "刷新 token")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    public CommonResult<TokenRespVO> refreshToken(@RequestParam String refreshToken) {
        return success(chuangKitService.refreshToken(refreshToken));
    }

    @PostMapping("/list-user-designs")
    @Operation(summary = "查询我的设计")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    public CommonResult<PageResult<GetUserDesignsRespVO>> getUserDesigns(@RequestBody @Valid GetUserDesignsReqVO reqVO) {
        return success(chuangKitService.getUserDesigns(reqVO));
    }

    @PostMapping("/delete-design")
    @Operation(summary = "删除,批量删除用户设计")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:delete')")
    public CommonResult<Boolean> deleteDesign(@RequestBody @Valid DeleteDesignReqVO reqVO) {
        return success(chuangKitService.deleteDesign(reqVO));
    }

    @PostMapping("/copy-design")
    @Operation(summary = "复制用户设计")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:update')")
    public CommonResult<CopyDesignRespVO> copyDesign(@RequestBody @Valid CopyDesignReqVO reqVO) {
        return success(chuangKitService.copyDesign(reqVO));
    }

    @PostMapping("/update-title")
    @Operation(summary = "修改设计名称")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:update')")
    public CommonResult<Boolean> updateTitle(@RequestBody @Valid UpdateTitleReqVO reqVO) {
        return success(chuangKitService.updateTitle(reqVO));
    }

    @PostMapping("/template-collection")
    @Operation(summary = "模板收藏接口")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:update')")
    public CommonResult<Boolean> templateCollection(@RequestBody @Valid TemplateCollectionReqVO reqVO) {
        return success(chuangKitService.templateCollection(reqVO));
    }

    @PostMapping("/template-cancel-collection")
    @Operation(summary = "模板取消收藏接口")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:update')")
    public CommonResult<Boolean> templateCancelCollection(@RequestBody @Valid TemplateCancelCollectionReqVO reqVO) {
        return success(chuangKitService.templateCancelCollection(reqVO));
    }

    @PostMapping("/list-template-collection")
    @Operation(summary = "查询收藏的模板")
//    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    @PermitAll
    public CommonResult<List<TemplateCollectionListRespVO>> getTemplateCollection(@RequestBody @Valid TemplateCollectionListReqVO reqVO) {
        return success(chuangKitService.getTemplateCollection(reqVO));
    }

    @PostMapping("/get-template-for-user")
    @Operation(summary = "获取创客贴官方模板")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    public CommonResult<TemplateForApiUserRespVO> queryTemplateForApiUser(@RequestBody @Valid TemplateForApiUserReqVO reqVO) {
        return success(chuangKitService.queryTemplateForApiUser(reqVO));
    }

    @PostMapping("/get-template-center-sign")
    @Operation(summary = "获取模板中心签名值")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    public CommonResult<String> getTemplateCenterSign(@RequestBody Map<String, String> params) {
        return success(chuangKitService.getTemplateCenterSign(params));
    }

    @PostMapping("/get-template-editor-sign")
    @Operation(summary = "获取模板中心签名值")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:query')")
    public CommonResult<String> getTemplateEditorSign(@RequestBody TreeMap<String, String> params) {
        return success(chuangKitService.getTemplateEditorSign(params));
    }

    @PostMapping("/request-package-id")
    @Operation(summary = "申请获取付费id")
    @PreAuthorize("@ss.hasPermission('system:chuang-kit:package-id')")
    public CommonResult<String> requestPackageId(){
        return success(chuangKitService.requestPackageId());
    }
}
